package ${groupId}.${artifactIdProjectPackageName}.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * js long 类型经度问题 转成字符串
 * @author lwd
 * @date 2020/11/19 16:47
 */
@Configuration
public class ObjectMapperConfig {

    @Autowired
    private ObjectMapper objectMapper;


    @PostConstruct
    public void init(){
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, new JsonSerializer<Long>() {
            @Override
            public void serialize(Long value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
                String text = (value == null ? null : String.valueOf(value));
                if (text != null) {
                    jsonGenerator.writeString(text);
                }
            }
        });
        objectMapper.registerModule(simpleModule);


    }

}
